<?php
class main extends spController
{
	
	public static $MENU_LIST = array(
			'beverage'=>array(
					'label'=>'BEVERAGE'
			),
			'food'=>array(
					'label'=>'FOOD'
			),
			'ibizatv'=>array(
					'label'=>'IBIZA TV'
			),
			'events'=>array(
					'label'=>'EVENTS'
			),
			'advertisement'=>array(
					'label'=>'ADVERTISEMENT'
			)
	);
	
	function index(){
		$userInfo = SessionUtil::getVar('userinfo');
		if ( $userInfo ) {
			$this->jump(spUrl('main', 'setcurrenttopmenu'));
		} else {
			$this->jump(spUrl('main', 'login'));
		}
	}
	function login(){
		$userObj = spClass("model_user");
		$uname = $this->spArgs("uname");
		if( $uname ){ // after submit form
			$upass = md5($this->spArgs("upass"));
			$userObj = spClass("model_user");
			// validate
			$rows = array('uname' => $uname, 'upass' => $upass);
			$results = $userObj->spVerifier($rows);
			
			$language_string = spClass("language_string");
			if( false == $results ){ // validate successfully, 
				$validateCode =  $this->spArgs("validatecode");
				if ( $validateCode  != SessionUtil::getVar('validatecode_login') ) {
					$this->error($language_string->getStr('login_code_error'), spUrl("main","login"));
				}
				if( false == $userObj->userlogin($uname, $upass) ){
					// login failure
					$this->error($language_string->getStr('login_failed'), spUrl("main","login"));
					
				}else{
					$this->success($language_string->getStr('login_success'), spUrl("main","setcurrenttopmenu"));
				}
				
			}else{
				foreach($results as $item){ 
					foreach($item as $msg){ 
						$this->error($msg,spUrl("main","login"));
					}
				}
			}//login check end
		}//post form end
	}
	
	public function logout(){
		// 这里是PHP.net关于删除SESSION的方法
		if (isset($_COOKIE[session_name()])) {setcookie(session_name(), '', time()-42000, '/');}
		spClass('spAcl')->set("");
		SessionUtil::destory();
		// 跳转回首页
		//$this->success("已退出，返回首页！", spUrl("main","index"));
		$this->jump(spUrl('main', 'login'));
	}
	
	public function showValidateCode(){
		$validateCodeObj = spClass("ValidateCode");
		$validateCodeObj->initValidateCode('validatecode_login');
	}
	public function changepassword(){
		$userInfo = SessionUtil::getVar('userinfo');
		$this->login_user_name = $userInfo ['uname'];
		
		$this->top_menu_list = main::$MENU_LIST;
		$this->current_top_menu_key = SessionUtil::getVar(SESSION_CURRENT_TOP_MENU_KEY);
		
		
		$sub = $this->spArgs("sub");
		if( $sub ){ // after submit form
			$old_password = $this->spArgs("old_password");
			if ( strlen($old_password) < 0 ) {
				$this->error("请填入旧密码", spUrl("main","changepassword"));
			}
			$new_password =  $this->spArgs("new_password");
			$new_password = trim($new_password);
			$new_password_confirm =  $this->spArgs("new_password_confirm");
			$new_password_confirm = trim($new_password_confirm);
			if ( $new_password != $new_password_confirm ) {
				$this->error("新密码与确定密码不相同", spUrl("main","changepassword"));
			}
			
			$userInfo = SessionUtil::getVar('userinfo');
			if ( md5($old_password) != $userInfo ['upass'] ) {
				$this->error("旧密码错误", spUrl("main","changepassword"));
			}
			$userObj = spClass("model_user");
			$userObj->changeUserPassowrld($userInfo['id'],$new_password);
			$this->success("修改管理员密码成功", spUrl("main","setcurrenttopmenu"));
			
		}
	}
	
	function setcurrenttopmenu(){
		$this->setcurrenttopmenu_core( $this->spArgs("menu_key"));
	}
	private function setcurrenttopmenu_core($menu_key = FALSE) {
		$menu_keys  = array_keys(self::$MENU_LIST);
		if ( !$menu_key ) {
			$menu_key = $menu_keys[0];
		}
		if ( !in_array($menu_key,$menu_keys) ) {
			$menu_key = $menu_keys[0];
		}
	
		SessionUtil::setVar(SESSION_CURRENT_TOP_MENU_KEY, $menu_key);
		

		$jump_controller = $menu_key;

		if ( $menu_key == "events" OR $menu_key == "advertisement" ) {
			$jump_controller = 'sliderimage';
			SessionUtil::setVar('current_slider_image_type',$menu_key);
		}

		$this->jump(spUrl($jump_controller,'index'));
	}
}